package clientView;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;

import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import utility.DayMonthYear;
import clientMain.Login;
import clientMain.Main;
import clientUtilGUI.KeyValue;
import clientUtilGUI.UtilityGUI;

import com.toedter.calendar.JMonthChooser;
import com.toedter.calendar.JYearChooser;

public class SS_Condition  extends JDialog implements ActionListener {
	
	private JPanel panel;
	private JLabel lblMonth;
	private JMonthChooser monthChooser;
	private JLabel lblYear;
	private JYearChooser yearChooser;
	private JLabel lblDep;
	private JComboBox cbbDep;
	private JLabel lblNote;
	private JButton btnOk;
	private JButton btnCancel;
	private JPanel panelBtn;
	private JRadioButton rdbtnTet;
	
	/**
	 * Create the frame.
	 */
	public SS_Condition(Main main) {
		super(main, UtilityGUI.tabSSList, true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		int height = 200;
	    int width = 350;
	    setSize(width, height);
	    setLocation((UtilityGUI.screenSize.width-width)/2, (UtilityGUI.screenSize.height-height)/2);
		setResizable(false);
		getContentPane().add(initComponent());
	}
	
	public void clear(){
		setVisible(false);
		getRootPane().removeAll();
		removeAll();
		dispose();
	}
	
	public JPanel initComponent(){
		panel = new JPanel(); 
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0};
		panel.setLayout(gridBagLayout);
		
		lblMonth = new JLabel("Tháng: ");
		lblMonth.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblDate = new GridBagConstraints();
		gbc_lblDate.anchor = GridBagConstraints.WEST;
		gbc_lblDate.fill = GridBagConstraints.BOTH;
		gbc_lblDate.insets = new Insets(15, 10, 5, 5);
		gbc_lblDate.gridx = 0;
		gbc_lblDate.gridy = 0;
		panel.add(lblMonth, gbc_lblDate);
		
		monthChooser = new JMonthChooser();
		monthChooser.setLocale(new Locale("vi"));
		monthChooser.getComboBox().setPreferredSize(new Dimension(120, 22));
		monthChooser.setFont(UtilityGUI.fontComboBox);
		GridBagConstraints gbc_monthChooser = new GridBagConstraints();
		gbc_monthChooser.insets = new Insets(15, 0, 5, 5);
		gbc_monthChooser.gridx = 1;
		gbc_monthChooser.gridy = 0;
		panel.add(monthChooser, gbc_monthChooser);
		
		lblYear = new JLabel("Năm: ");
		lblYear.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblYear = new GridBagConstraints();
		gbc_lblYear.insets = new Insets(15, 5, 5, 5);
		gbc_lblYear.fill = GridBagConstraints.BOTH;
		gbc_lblYear.gridx = 2;
		gbc_lblYear.gridy = 0;
		panel.add(lblYear, gbc_lblYear);
		
		yearChooser = new JYearChooser();
		yearChooser.setFont(UtilityGUI.fontComboBox);
		yearChooser.setEndYear(Main.fullday.getYear()+1900);
		yearChooser.setStartYear(2000);
		GridBagConstraints gbc_yearChooser = new GridBagConstraints();
		gbc_yearChooser.anchor = GridBagConstraints.WEST;
		gbc_yearChooser.insets = new Insets(15, 0, 5, 10);
		gbc_yearChooser.gridx = 3;
		gbc_yearChooser.gridy = 0;
		panel.add(yearChooser, gbc_yearChooser);
		
		rdbtnTet = new JRadioButton("Tháng 13(Tết)");
		rdbtnTet.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_rdbtnTet = new GridBagConstraints();
		gbc_rdbtnTet.anchor = GridBagConstraints.WEST;
		gbc_rdbtnTet.gridwidth = 4;
		gbc_rdbtnTet.insets = new Insets(3, 10, 5, 5);
		gbc_rdbtnTet.gridx = 0;
		gbc_rdbtnTet.gridy = 1;
		panel.add(rdbtnTet, gbc_rdbtnTet);
		rdbtnTet.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				AbstractButton aButton = (AbstractButton)e.getSource();
		        ButtonModel aModel = aButton.getModel();
		        boolean selected = aModel.isSelected();
		        if(selected){
		        	monthChooser.setEnabled(false);
		        }else{
		        	monthChooser.setEnabled(true);
		        }
			}
		});
		
		lblDep = new JLabel("Phòng ban:");
		lblDep.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblEmName = new GridBagConstraints();
		gbc_lblEmName.anchor = GridBagConstraints.WEST;
		gbc_lblEmName.insets = new Insets(3, 10, 5, 5);
		gbc_lblEmName.gridx = 0;
		gbc_lblEmName.gridy = 2;
		panel.add(lblDep, gbc_lblEmName);
		
		cbbDep = UtilityGUI.getComboBox("Department", 0,"");
		GridBagConstraints gbc_cbbDep = new GridBagConstraints();
		gbc_cbbDep.insets = new Insets(3, 0, 5, 10);
		gbc_cbbDep.anchor = GridBagConstraints.WEST;
		gbc_cbbDep.fill = GridBagConstraints.BOTH;
		gbc_cbbDep.gridx = 1;
		gbc_cbbDep.gridy = 2;
		gbc_cbbDep.gridwidth = 3;
		panel.add(cbbDep, gbc_cbbDep);
		
		panelBtn = new JPanel();
		GridBagConstraints gbc_panelBtn = new GridBagConstraints();
		gbc_panelBtn.gridwidth = 4;
		gbc_panelBtn.fill = GridBagConstraints.BOTH;
		gbc_panelBtn.anchor = GridBagConstraints.CENTER;
		gbc_panelBtn.insets = new Insets(5, 10, 5, 10);
		gbc_panelBtn.gridx = 0;
		gbc_panelBtn.gridy = 3;
		panel.add(panelBtn, gbc_panelBtn);
		
		btnOk = new JButton(UtilityGUI.btnOk);
		btnOk.setFont(UtilityGUI.fontBtnForm);
		btnOk.addActionListener(this);
		panelBtn.add(btnOk);
		
		btnCancel = new JButton(UtilityGUI.btnCancel);
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		btnCancel.addActionListener(this);
		panelBtn.add(btnCancel);
		
		lblNote = new JLabel("** Hãy chọn tháng năm và phòng ban mà bạn muốn xem **");
		lblNote.setFont(UtilityGUI.fontNote);
		GridBagConstraints gbc_lblNote = new GridBagConstraints();
		gbc_lblNote.anchor = GridBagConstraints.NORTH;
		gbc_lblNote.gridwidth = 4;
		gbc_lblNote.insets = new Insets(5, 10, 10, 10);
		gbc_lblNote.gridx = 0;
		gbc_lblNote.gridy = 4;
		panel.add(lblNote, gbc_lblNote);
		
		return panel;
	}
		
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnOk){
			String monthYear;
			int depId = 0;
			String depName = null;
			boolean flag = false;
			if(monthChooser.getMonth() > Main.fullday.getMonth() && yearChooser.getYear() == Main.fullday.getYear()+1900){
				JOptionPane.showMessageDialog(null, "Ko chọn thời điểm tương lai!!!", 
								UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				monthChooser.getComboBox().requestFocusInWindow();
				return;
			}else{
				monthYear = DayMonthYear.monthYear(monthChooser.getMonth()+1, yearChooser.getYear());
			}
			if(cbbDep.getSelectedIndex() == 0){
				cbbDep.requestFocusInWindow();
				return;
			}else if(cbbDep.getSelectedIndex() != 0){
				depId = Integer.parseInt(((KeyValue)cbbDep.getSelectedItem()).getKey());
				for(int i=0; i<Login.listAuthorization.size(); i++){
					if(Login.listAuthorization.get(i).getModuleDep() == depId){
						depName = ((KeyValue)cbbDep.getSelectedItem()).getValue();
						flag = true;
						break;
					}
				}
			}
			if(flag == false){
				JOptionPane.showMessageDialog(null, UtilityGUI.errorAuth, 
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				cbbDep.requestFocusInWindow();
				return;
			}else{
				clear();
				int index = Main.jTabbedPane.positionTab(UtilityGUI.tabSSList);
				if(index == -1){
					Main.jTabbedPane.addTab(new SS_List(depId, depName, monthYear), UtilityGUI.tabSSList);
					Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
				}else{ 
					Main.jTabbedPane.removeTabAt(index);
					Main.jTabbedPane.addTab(new SS_List(depId, depName, monthYear), UtilityGUI.tabSSList);
					Main.jTabbedPane.setSelectedIndex(index);
				}
				
			}
		}else if(e.getSource() == btnCancel){
			clear();
		}
	}	
}

